package com.orhon.smartcampus.modules.schoolaffairs.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Dictionary;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLDictionary;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.DictionaryMapper;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolNewsDao;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolNews;
import com.orhon.smartcampus.modules.schoolaffairs.gqlModel.GQLSchoolNews;
import com.orhon.smartcampus.modules.schoolaffairs.nm.SchoolNewsMapper;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class SchoolNewssFetcher implements DataFetcher {
    @Autowired
    TokenService tokenService;

    @Autowired
    SchoolNewsDao schoolNewsDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
        String hql = "from SchoolNews where deleted_at = null "+" and school_id = " + schoolId;
        List<GQLSchoolNews> GQLSchoolNewNotice = new ArrayList<>();
        if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
            if(map.get("type_id")!=null) {
                hql += " and type" + Util.decide(map.get("type"));
            }
            if(map.get("eclass_id")!=null) {
                hql += " and eclass" + Util.decide(map.get("eclass_id"));
            }
            if(map.get("departments_id")!=null) {
                hql += " and departments" + Util.decide(map.get("departments_id"));
            }
            if(map.get("title")!=null){
                hql += " and title like '%"+map.get("title")+"%'";
            }
        }
        List<SchoolNews> list = schoolNewsDao.find(hql, null, page.getPage(), page.getLimit());
        for(SchoolNews news:list){
            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("data/school")){
                news.getSchoolsBySchool();
                Schools schools = news.getSchoolsBySchool();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }
            GQLEclass gqlEclass=null;
            if(env.getSelectionSet().contains("data/eclass")){
                news.getEclass();
                Eclass eclass = news.getEclass();
                gqlEclass = EclassMapper.MAPPER.toEclass(eclass);
            }
            GQLUser gqlUser = null;
            if(env.getSelectionSet().contains("data/user")){
                Users publisher = news.getPublisher();
                gqlUser= UserMapper.MAPPER.mapUser(publisher);
            }
            GQLDictionary gqlDictionary = null;
            if(env.getSelectionSet().contains("data/dictinoary")){
                Dictionary publisher = news.getType();
                gqlDictionary= DictionaryMapper.MAPPER.toDictionary(publisher);
            }
            GQLSchoolNews gqlSchoolNewNotice = SchoolNewsMapper.MAPPER.toSchoolNews(news);
            gqlSchoolNewNotice.setEclass(gqlEclass);
            gqlSchoolNewNotice.setSchool(gqlSchool);
            gqlSchoolNewNotice.setPublisher(gqlUser);
            gqlSchoolNewNotice.setType(gqlDictionary);
            GQLSchoolNewNotice.add(gqlSchoolNewNotice);
        }
        int total = 0;
        if (env.getSelectionSet().contains("total")){
            total = schoolNewsDao.total(hql,null);
        }
        return Result.ok().put("data", GQLSchoolNewNotice).put("total", total);
    }
}
